package com.etc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.Provider.Service;
import java.sql.Array;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceConfigurationError;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.omg.CORBA.RepositoryIdHelper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.etc.dao.OrderDetailDao;
import com.etc.dao.PersonDao;
import com.etc.dao.ProductInfoDao;
import com.etc.dao.impl.OrderDetailDaoImpl;
import com.etc.dao.impl.PersonDaoImpl;
import com.etc.dao.impl.ProductInfoDaoImpl;
import com.etc.entity.OrderInfo;
import com.etc.entity.Person;
import com.etc.entity.ProductInfo;
import com.etc.service.OrderDetailService;
import com.etc.service.impl.OrderDetailServiceImpl;

public class checkoutServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public checkoutServlet() {
		super();
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
 		String s=JSON.parseArray(request.getParameter("productlist")).toJSONString();
		List list = JSON.parseArray(request.getParameter("productlist"));
		PersonDao pdao=new PersonDaoImpl();
		double buybalance=0;//用户购买商品后的余额
		HttpSession session = request.getSession();
		Person person = (Person) session.getAttribute("person");
		int uid = person.getId();//用户id
		OrderDetailService service=new OrderDetailServiceImpl();
		    OrderInfo orderInfo =null;
			orderInfo=service.checkOut(uid, list);
		//  buybalance=pdao.findPersonById(uid).getBalance();
			if(orderInfo!=null){
				String jsonStr = JSON.toJSONString(orderInfo);
				out.print(jsonStr);
			}else{
				out.print("余额不足");
			}

			out.flush();
			out.close();
		
	}

}
